<template>
  <q-dialog v-model="qrMappingDiag">
    <QRMappingDiag :qr_string="qr_string" @onSubmit="qrMappingDiag = false"  @cancel="qrMappingDiag = false"></QRMappingDiag>
  </q-dialog>
  <div class="q-pa-sm my-card">
    <div class="page-header q-pa-sm fal">
      <q-item-section avatar> <q-icon name="construction" /> </q-item-section
      >Maintenance
    </div>
    <q-card class="q-pa-sm search-css card-radius">
      <div class="row">
        <div class="text-sm q-pa-sm col-5">
          <div class="row">
            <div class="col-4 fac">Stock Code:</div>
            <div class="col-8">
              <q-input
                input-class="input-css"
                borderless
                dense
                v-model="stock_code"
              ></q-input>
            </div>
          </div>
        </div>

        <div class="text-sm q-pa-sm col-5">
          <div class="row">
            <div class="col-4 fac">Serial No:</div>
            <div class="col-8">
              <q-input
                input-class="input-css"
                borderless
                dense
                v-model="serial_no"
              ></q-input>
            </div>
          </div>
        </div>
        <div class="text-sm q-pa-sm col-2 fac">
          <q-icon
            name="qr_code"
            size="40px"
            color="primary"
            @click="scanDiag = true"
          ></q-icon>
          <q-dialog v-model="scanDiag">
            <OpenCam @submit="(v) => onSubmit(v)"></OpenCam>
          </q-dialog>
        </div>
        <div v-if="msg" class="warning-text">
          {{ 'NO RECORD. PLEASE TRY AGAIN OR CREATE ORDER FIRST' }}
        </div>
        
        <div class="text-sm q-pa-sm col-5">
          <div class="row">
            <div class="col-4 fac">Short Description:</div>
            <div class="col-8">
              <q-input
                input-class="input-css"
                borderless
                dense
                v-model="short_description"
              ></q-input>
            </div>
          </div>
        </div>
        <div class="text-sm q-pa-sm col-5">
          <div class="row">
            <div class="col-4 fac">M'tce Type:</div>
            <div class="col-8">
            <q-select
              class="input-css"
              :dense="true"
              borderless
              v-model="mtce_type"
              :options="['IGI','CM','PM','NM']"
              use-input
            ></q-select>
          </div>
          </div>
        </div>
        <div class="text-sm q-pa-sm col-5">
          <div class="row">
            <div class="col-4 fac">Status:</div>
            <div class="col-8">
            <q-select
              class="input-css"
              :dense="true"
              borderless
              v-model="status"
              :options="['WAITING_FOR_JOB_COMPLETED', 'WAITING_FOR_SIC','WAITING_FOR_ENDORSEMENT','IN_PROCESS', 'CHECKED IN']"
              use-input
            ></q-select>
          </div>
          </div>
        </div>
        <div class="text-sm q-pa-md fac">
          <q-checkbox
            dense
            v-model="complete"
            size="60px"
            label="Job Complete"
          />
        </div>
        <q-card-actions align="center" class="action col-12">
          <q-btn class="default-btn" @click="search">Search</q-btn>
        </q-card-actions>
      </div>

    </q-card>
    <div v-if="!msg ">
      <JOListTable :data="data"></JOListTable>
    </div>
  </div>
</template>

<script setup lang="ts">
import { searchPart } from 'src/services/partsService';
import { ref } from 'vue';
import { Parts, RepairOrder } from 'src/services/type';
import JOListTable from 'src/components/maintenance/JOListTable.vue';
import { useCheckinStore } from 'src/stores/checkinStore';
import { usePartsStore } from 'src/stores/partsStore';
import OpenCam from 'src/components/OpenCam.vue';
import { searchOrderParts } from 'src/services/repairService';
import { parsePartQR } from 'src/utils/parseQRCodeUtils';
import QRMappingDiag from 'src/components/QRMappingDiag.vue'

const stock_code = ref('');
const serial_no = ref('');
const qr_string = ref('')
const status = ref('');
const mtce_type = ref('');
const short_description = ref('')
const complete = ref(false);
const qrMappingDiag = ref(false)
const data = ref<
  {
    id: number;
    stock_code: string;
    status: string;
    mtce_type: string;
    short_description: string;
    serial_no: string;
    repair_order: RepairOrder;
  }[]
>([]);
const scanDiag = ref(false);
const msg = ref(false);
const useCheckin = useCheckinStore();
const useParts = usePartsStore();

async function search() {
  msg.value=true
  const res = await searchOrderParts(
    stock_code.value,
    serial_no.value,
    true,
    false,
    complete.value,
    status.value||'',
    mtce_type.value||'',
    short_description.value
  );
  console.log(res.data);
  data.value = res.data
  // data.value = res.data.filter((item) => {
  //   if (complete.value) {
  //     return item.repair_order.status.includes('COMPLETE');
  //   }
  //   return !item.repair_order.status.includes('COMPLETE');
  // });
  if (data.value.length == 0) {
    return (msg.value = true);
  }
  msg.value = false;
  // if (stock_code.value && serial_no.value && complete.value) {
  //   const res = await searchOrderParts(stock_code.value, serial_no.value);
  //   console.log(res.data);
  //   if (res.data.length == 0) {
  //     return (msg.value = true);
  //   }
  //   data.value = res.data;
  //   msg.value = false;
  // } else if (stock_code.value) {
  //   const res = await searchOrderParts(stock_code.value, '');
  //   console.log(res.data);
  //   if (res.data.length == 0) {
  //     return (msg.value = true);
  //   }
  //   data.value = res.data;
  //   msg.value = false;
  // }
}

async function onSubmit(v: string) {
  scanDiag.value = false;
  const obj = await parsePartQR(v);  
  if(obj){
    stock_code.value = obj.CAT;
    serial_no.value = obj.SN;
    scanDiag.value = false;
  }
  // if(!obj){
  //   qr_string.value = v
  //   qrMappingDiag.value = true
  // }
  search();
}

useParts.clearParts();
useCheckin.clear();
search();
</script>

<style>
.content {
  display: flex;
  justify-content: space-between;
  background-color: #eef4fa;
  padding-top: 20px;
  padding-left: 20px;
  padding-right: 10px;
}

.warning-text {
  color: red;
  font-weight: bold;
  text-align: center;
  width: -webkit-fill-available;
  padding-top: 20px;
}
</style>
